﻿using LS.Screening.UI.Common.Controls;
using System.Globalization;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace LS.Screening.UI.Common
{
    public static class WindowExtensions
    {
        /// <summary>
        /// 注册KeyDown的事件触发到本控件
        /// </summary>
        /// <param name="control"></param>
        public static void RegistKeyDownEvent(this UserControl control)
        {
            var parent = control.FindParent<Window>();
            if (parent == null) return;
            var hasKeyDownMethod = control.GetType().GetMethod("OnKeyDown", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic) != null;
            if (hasKeyDownMethod == false) return;
            KeyEventHandler KeyDown = (a, e) =>
            {
                try
                {
                    if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
                    {
                        control.RaiseEvent(e);
                    }
                }
                catch
                {
                    e.Handled = true;
                }
            };
            parent.KeyDown += KeyDown;

            control.Unloaded += (c, d) =>
            {
                parent.KeyDown -= KeyDown;
            };
        }

        /// <summary>
        /// 弹窗显示控件
        /// </summary>
        /// <param name="control"></param>
        /// <param name="title"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="initAction"></param>
        /// <returns></returns>
        public static CurrentWindow ShowDialog(this UserControl control, string title, int width = 1280, int height = 768, Action<CurrentWindow> initAction = null)
        {
            var window = GetWindow();
            window.Width = width;
            window.Height = height;
            window.Title = title;
            window.Content = control;
            window.SizeToContent = SizeToContent.Manual;
            initAction?.Invoke(window);
            window.ShowDialog();
            return window;
        }

        public static CurrentWindow GetWindow()
        {
            var window = new CurrentWindow();
            window.SetOwnerWindow();
            window.Width = 1280;
            window.Height = 800;
            window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            var culture = CultureInfo.GetCultureInfo("zh-CN");
            CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = culture;
            return window;
        }

        public static void SetOwnerWindow(this Window window)
        {
            for (int i = Application.Current.Windows.Count - 1; i >= 0; i--)
            {
                var win = Application.Current.Windows[i];
                if (win.IsActive && win != window)
                {
                    window.Owner = win;
                    break;
                }
            }
        }

        public static void ToOwnerCenter(this Window window, double width, double height)
        {
            if (width > 0 && height > 0)
            {
                ToOwnerCenter(window, new Size(width, height));
            }
            else
            {
                ToOwnerCenter(window);
            }
        }

        public static void ToOwnerCenter(this Window window, Size? size = null)
        {
            if (window.Owner == null) return;
            if (size != null)
            {
                window.Width = size.Value.Width;
                window.Height = size.Value.Height;
            }
            var left = (window.Owner.ActualWidth - window.Width) / 2;
            var top = (window.Owner.ActualHeight - window.Height) / 2;
            if (left >= 0 || top >= 0)
            {
                window.Left = left;
                window.Top = top;
            }
            else
            {
                window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }
        }
    }
}
